var datewang = new Date();
var year = datewang.getFullYear();
var month = datewang.getMonth() + 1;
var today = datewang.getDate();

// pages/registered/registered.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    regisList: [{
        title: "医院名称",
        text: "请输入医院名称",
        names: "ytitle"
      },
      {
        title: "科室名称",
        text: "请输入科室名称",
        names: "ketitle"
      },
      {
        title: "医生名称",
        text: "请输入医生名称",
        names: "ystitle"
      }
    ],
    date: year + "-" + month + "-" + today,
    time: '12:01',
    Show: "none",
  },
  // 日期选择
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  // 时间选择
  bindTimeChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  // 确定预约
  formRegistered: function(options) {
    var that = this
    var url = app.util.url('entry/wxapp/reg')
    var urlpay = app.util.url('entry/wxapp/regstate')
    var values = options.detail.value
    var _openid = wx.getStorageSync("openid");
    var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
    var idcardreg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    if (phoneReg.test(values.callnumber)){
      wx.showToast({
        title: '请输入正确的手机号',
        icon: "none"
      })
      return
    }
    if (idcardreg.test(values.idCard)){
      wx.showToast({
        title: '请输入正确的身份证号码',
        icon: "none"
      })
      return
    }
    console.log("取值========>", options.detail.value)
    if (values.ytitle == undefined || values.ytitle == '' || values.ystitle == undefined || values.ystitle == "" || values.ketitle == undefined || values.ketitle == '') {
      wx.showToast({
        title: '请输入完整信息',
        icon: "none"
      })
    } else if (values.callperson == undefined || values.callperson == "" || values.callnumber == undefined || values.callnumber == "") {
      wx.showToast({
        title: '请输入完整信息',
        icon: "none"
      })
    } else if (values.jiuzhen == undefined || values.jiuzhen == "" || values.idCard == undefined || values.idCard == "") {
      wx.showToast({
        title: '请输入完整信息',
        icon: "none"
      })
    } else {
    app.util.request({
      url: url,
      data: {
        m: 'md_daojia',
        uid: wx.getStorageSync('userInfo'),
        hospitalname: values.ytitle,
        department: values.ketitle,
        doctorname: values.ystitle,
        pre_time: that.data.date,
        name: values.callperson,
        tel: values.callnumber,
        disease_name: values.jiuzhen,
        disease_num: values.idCard,
        openid: _openid,
      },
      success: function(t) {
        var show = "";
        console.log("预约挂号=========>", t);
        wx.requestPayment({
          timeStamp: t.data.data.timeStamp,
          nonceStr: t.data.data.nonceStr,
          package: t.data.data.package,
          signType: t.data.data.signType,
          paySign: t.data.data.paySign,
          success: function (res) {
            console.log("这里是支付成功", res)
            app.util.request({
              url: urlpay,
              cachetime: "0",
              data: {
                m: 'md_daojia',
                orderid: t.data.data.id
              },
              success: function (t) {
                console.log("改变订单状态", t)
                wx.showToast({
                  title: "付款成功",
                  icon: "none",
                  image: "",
                  duration: 2e3,
                  mask: !0,
                  success: function (t) { },
                  fail: function (t) { },
                  complete: function (t) { }
                }), setTimeout(function () {
                  wx.redirectTo({
                    url: '/pages/index/index'
                  })
                }, 2e3);
              }
            });
          },
          fail: function (res) {
            wx.showToast({
              title: "支付失败",
              duration: 1e3
            });
          }
        });
        // var list = string(res.data)
      //  that.data.Show == "none" ? show = "block":show = "none"
      //   that.setData({
      //     Show: show
      //   }) 
      }
    })

    }
  },

  close: function(){
    var hide = ""
    this.data.Show == "block" ? hide = "none" : hide = "none"
    this.setData({
        Show: hide
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})